package com.magenta.maxoptra.gis.distancematrix;

import java.util.Comparator;

/**
 * @author Sergey Grachev
 */
final class CommandsComparator implements Comparator<Command> {
    private static final CommandsComparator INSTANCE = new CommandsComparator();

    private CommandsComparator() {
    }

    public static CommandsComparator getInstance() {
        return INSTANCE;
    }

    @Override
    public int compare(final Command o1, final Command o2) {
        final float p1 = o1.getType().getPriorityOrder(), p2 = o2.getType().getPriorityOrder();
        final boolean isPrioritiesEquals = p1 == p2;
        if (isPrioritiesEquals && o1 instanceof GroupCommand && o2 instanceof GroupCommand) {
            final long g1 = ((GroupCommand) o1).getGroupId(), g2 = ((GroupCommand) o2).getGroupId();
            return g1 == g2 ? 0 : (g1 > g2 ? 1 : -1);
        } else {
            return isPrioritiesEquals ? 0 : (p1 > p2 ? 1 : -1);
        }
    }
}
